package test
{

import flexunit.framework.TestCase;
import flexunit.framework.TestSuite;

import com.fit4as.fit.Parse;
import com.fit4as.fit.ParseException;

public class ParseTest extends TestCase
{

    public function ParseTest(methodName:String) {
        super(methodName);
    }

    public static function suite():TestSuite {
        var ts:TestSuite = new TestSuite();
        ts.addTest(new ParseTest("testInstantiating"));
        ts.addTest(new ParseTest("testParsing"));
        ts.addTest(new ParseTest("testRecursing"));
        ts.addTest(new ParseTest("testIterating"));
        ts.addTest(new ParseTest("testIndexing"));
        ts.addTest(new ParseTest("testParseException"));
        ts.addTest(new ParseTest("testText"));
        ts.addTest(new ParseTest("testUnescape"));
        ts.addTest(new ParseTest("testWhitespaceIsCondensed"));
        return ts;
    }

    public function testInstantiating() : void
    {
        var p : Parse = Parse.createParseFromStrings("Table foo=2", "body", null, null);
        assertNull(p.parts);
        assertEquals("<Table foo=2>", p.tag);
        assertEquals("body", p.body);
        assertNull(p.more);
    }

    public function testParsing() : void {
        var p : Parse = Parse.createParse("leader<Table foo=2>body</table>trailer", ["table"]);
        assertNotNull(p);
        assertEquals("leader", p.leader);
        assertEquals("<Table foo=2>", p.tag);
        assertEquals("body", p.body);
        assertEquals("trailer", p.trailer);
    }

    public function testRecursing() : void {
        var p : Parse = Parse.createParse("leader<table><TR><Td>body</tD></TR></table>trailer");

        assertEquals(null, p.body);
		//assertEquals(null, p.parts.body);
        //assertEquals("body", p.parts.parts.body);
    }

    public function testIterating() : void {
        var p : Parse = Parse.createParse("leader<table><tr><td>one</td><td>two</td><td>three</td></tr></table>trailer");
        assertEquals("one", p.parts.parts.body);
        assertEquals("two", p.parts.parts.more.body);
        assertEquals("three", p.parts.parts.more.more.body);
    }

    public function testIndexing() : void {
        var p : Parse = Parse.createParse("leader<table><tr><td>one</td><td>two</td><td>three</td></tr><tr><td>four</td></tr></table>trailer");
        assertEquals("one", p.at(0, 0, 0).body);
        assertEquals("two", p.at(0, 0, 1).body);
        assertEquals("three", p.at(0, 0, 2).body);
        assertEquals("three", p.at(0, 0, 3).body);
        assertEquals("three", p.at(0, 0, 4).body);
        assertEquals("four", p.at(0, 1, 0).body);
        assertEquals("four", p.at(0, 1, 1).body);
        assertEquals("four", p.at(0, 2, 0).body);
        assertEquals(1, p.size());
        assertEquals(2, p.parts.size());
        assertEquals(3, p.parts.parts.size());
        assertEquals("one", p.leaf().body);
        assertEquals("four", p.parts.last().leaf().body);
    }

    public function testParseException() : void {
        try {
            trace("\n--------------------------------- \nthe exception below is expected, see testParseException");
            var p : Parse = Parse.createParse("leader<table><tr><th>one</th><th>two</th><th>three</th></tr><tr><td>four</td></tr></table>trailer");
        } catch (e : ParseException) {
            trace("the exception above is expected, see testParseException.\n--------------------------------- \n");
            assertEquals(17, e.getErrorOffset());
            assertEquals("Can't find tag: td", e.getMessage());
            return;
        }
        fail("exptected exception not thrown");
    }

    public function testText() : void {
        var tags : Array = ["td"];
        var p : Parse = Parse.createParse("<td>a&lt;b</td>", tags);
        assertEquals(1, "a&lt;b", p.body);
        assertEquals(2, "a<b", p.text());
        p = Parse.createParse("<td>\ta&gt;b&nbsp;&amp;&nbsp;b>c &&&lt;</td>", tags);
        assertEquals(3, "a>b & b>c &&<", p.text());
        p = Parse.createParse("<td>\ta&gt;b&nbsp;&amp;&nbsp;b>c &&lt;</td>", tags);
        assertEquals(4, "a>b & b>c &<", p.text());
        p = Parse.createParse("<TD><P><FONT FACE=\"Arial\" SIZE=2>GroupTestFixture</FONT></TD>", tags);
        assertEquals(5, "GroupTestFixture", p.text());

        assertEquals(6, "", Parse.htmlToText("&nbsp;"));
        assertEquals(7, "a b", Parse.htmlToText("a <tag /> b"));
        assertEquals(8, "a", Parse.htmlToText("a &nbsp;"));
        assertEquals(9, "&nbsp;", Parse.htmlToText("&amp;nbsp;"));
        assertEquals(10, "1     2", Parse.htmlToText("1 &nbsp; &nbsp; 2"));
        assertEquals(11, "1     2", Parse.htmlToText("1 \u00a0\u00a0\u00a0\u00a02"));
        assertEquals(12, "a", Parse.htmlToText("  <tag />a"));
        assertEquals(13, "a\nb", Parse.htmlToText("a<br />b"));

        assertEquals(14, "ab", Parse.htmlToText("<font size=+1>a</font>b"));
        assertEquals(15, "ab", Parse.htmlToText("a<font size=+1>b</font>"));
        assertEquals(16, "a<b", Parse.htmlToText("a<b"));

        assertEquals(17, "a\nb\nc\nd", Parse.htmlToText("a<br>b<br/>c<  br   /   >d"));
        assertEquals(18, "a\nb", Parse.htmlToText("a</p><p>b"));
        assertEquals(19, "a\nb", Parse.htmlToText("a< / p >   <   p  >b"));
    }

    public function testUnescape() : void {
        assertEquals(1, "a<b", Parse.unescape("a&lt;b"));
        assertEquals(2, "a>b & b>c &&", Parse.unescape("a&gt;b&nbsp;&amp;&nbsp;b>c &&"));
        assertEquals(3, "&amp;&amp;", Parse.unescape("&amp;amp;&amp;amp;"));
        assertEquals(4, "a>b & b>c &&", Parse.unescape("a&gt;b&nbsp;&amp;&nbsp;b>c &&"));
        assertEquals(5, "\"\"'", Parse.unescape("“”’"));
    }

    public function testWhitespaceIsCondensed() : void {
        assertEquals(1, "a b", Parse.condenseWhitespace(" a  b  "));
        assertEquals(2, "a b", Parse.condenseWhitespace(" a  \n\tb  "));
        assertEquals(3, "", Parse.condenseWhitespace(" "));
        assertEquals(4, "", Parse.condenseWhitespace("  "));
        assertEquals(5, "", Parse.condenseWhitespace("   "));
        assertEquals(6, "", Parse.condenseWhitespace(String.fromCharCode(160)));
    }



}
}